package com.ncepu.clubmanagement.pojo;


import com.ncepu.clubmanagement.exception.AccessDeniedException;
import com.ncepu.clubmanagement.pojo.inter.IsClubManagerCheck;
import com.ncepu.clubmanagement.pojo.inter.MessageAssist;
import com.ncepu.clubmanagement.pojo.inter.ToMessageLinkAssist;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@AllArgsConstructor
@NoArgsConstructor
@Data
public class MessageFont implements MessageAssist {


    private String messageContent;
    private String messageTitle;
    private Integer targetClubId;
    private Integer targetPersonId;
    private Integer sendType;        // 0 私发 1 群发
    private Integer sourcePersonId;  // 不用传, 由系统自动填充

    public Message toMessage(){
        return new Message(null,messageContent,messageTitle,LocalDateTime.now()
                ,targetClubId,sourcePersonId,sendType);
    }

    public List<MessageLink> toMessageLinks(ToMessageLinkAssist assist, Integer messageId){
        List<MessageLink> ls = new ArrayList<MessageLink>();
        if (this.sendType.equals(MESSAGE_CLUB_HAIR_TYPE))   // 群发
        {
            List<Integer> personIds = assist.getPersonIdByClubId(this.targetClubId);
            for (Integer personId : personIds)
            {
                ls.add(new MessageLink(personId,messageId,0));
            }
        }
        else if (this.sendType.equals(MESSAGE_PRIVATE_HAIR_TYPE))  // 私发
        {
            ls.add(new MessageLink(this.targetPersonId,messageId,0));

        }
        return ls;
    }

    public List<MessageLink> toMessageLinksWithPermissionCheck(ToMessageLinkAssist assist, IsClubManagerCheck isClubManagerCheck, Integer messageId)throws Exception{
        List<MessageLink> ls = new ArrayList<MessageLink>();
        if (this.sendType.equals(MESSAGE_CLUB_HAIR_TYPE))   // 群发
        {

            // 权限分析
            if (! isClubManagerCheck.isClubManager(targetClubId))
                throw new AccessDeniedException();
            // 群发逻辑
            List<Integer> personIds = assist.getPersonIdByClubId(this.targetClubId);
            for (Integer personId : personIds)
            {
                ls.add(new MessageLink(personId,messageId,0));
            }
        }
        else if (this.sendType.equals(MESSAGE_PRIVATE_HAIR_TYPE))  // 私发
        {
            ls.add(new MessageLink(this.targetPersonId,messageId,0));

        }
        return ls;
    }



}
